/**
 * com.future CO.,ltd.
 */

package com.future.springcloud.filter;

import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * 类说明：
 */
@Component
public class MyLogGateWayFilter implements GlobalFilter, Ordered {

	/**
	 * 定义拦截，统一的拦截类，
	 * 符合条件才继续下去
	 *
	 * @param exchange
	 * @param chain
	 * @return
	 */
	@Override
	public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {

		/**
		 * 说明：是放在url末尾，如：localhost:9527/payment/get/30028?username=3
		 * 而不是放到header里
		 */
		String username = exchange.getRequest().getQueryParams().getFirst("username");
		/*if (StringUtils.isEmpty(username)) {
			System.out.println("非法用户");
			exchange.getResponse().setStatusCode(HttpStatus.NOT_ACCEPTABLE);
			exchange.getResponse().setComplete();
		}*/
		return chain.filter(exchange);
	}

	@Override
	public int getOrder() {
		return 0;
	}
}
